<?php
//声明命名空间
namespace Admin\Controller;
use \Frame\Libs\BaseController;
use \Admin\Model\CategoryModel;

final class CategoryController extends BaseController
{
    public function index()
    {
        //验证是否登录
        $this->denyAccess();
        //获取分类原数据
        $Categorys = CategoryModel::getInstance()->fetchAll();

        //获取无限极分类数据(调用CategoryList方法)
        $Categorys = CategoryModel::getInstance()->CategoryList($Categorys);

        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->display("Category/index.html");
    }
    public function del()
    {
        //验证是否登录
        $this->denyAccess();
        $id = $_GET["id"];
        $category = CategoryModel::getInstance()->del($id);
        if($category){
            $this->jump("删除记录为{$id}的数据成功","?c=Category");
        }else{
            $this->jump("删除记录为{$id}的数据成功","?c=Category");
        }
    }
    public function update()
    {
        //验证是否登录
        $this->denyAccess();
        $id = $_GET["id"];
        //根据id查找数据
        $arr = CategoryModel::getInstance()->fetchOne("id=$id");
        //无限极分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
            CategoryModel::getInstance()->fetchAll()
        );
        //视图赋值
        $this->smarty->assign("arr",$arr);
        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->display("Category/update.html");
    }
    public function edit()
    {
        //验证是否登录
        $this->denyAccess();
        $id = $_POST["id"];
        $classname = $_POST["classname"];
        $orderby = $_POST["orderby"];
        $pid = $_POST["pid"];

        $result = CategoryModel::getInstance()->edit("classname='$classname',orderby='$orderby',pid='$pid'",$id);
        if($result){
            $this->jump("修改记录为{$id}的数据成功","?c=Category");
        }else{
            $this->jump("修改记录为{$id}的数据成功","?c=Category");
        }
    }
    public function add()
    {
        //验证是否登录
        $this->denyAccess();
        //无限极分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
            CategoryModel::getInstance()->fetchAll()
        );
        $this->smarty->assign("categorys",$Categorys);
        $this->smarty->display("Category/add.html");
    }
    public function insert()
    {
        //验证是否登录
        $this->denyAccess();
        $data["classname"] = $_POST["classname"];
        $data["orderby"] = $_POST["orderby"];
        $data["pid"] = $_POST["pid"];

        $result = CategoryModel::getInstance()->insert($data);
        if($result){
            $this->jump("添加分类成功","?c=Category");
        }else{
            $this->jump("添加分类失败","?c=Category");
        }
    }
}
